package ca.mmyo.customFontTextViewer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class customFontTextViewer extends Activity {
	/** Called when the activity is first created. */
	
	private CharSequence[] fontPaths = {"fonts/zawgyione.ttf", "fonts/DejaVuSans.ttf", "fonts/Padauk.ttf", "fonts/DejaVuSerif.ttf", "fonts/DejaVuSansMono.ttf", "fonts/mm3.ttf"};
	private CharSequence[] fontNames = {"Zawgyi One", "DejaVu Sans", "Padauk", "DejaVu Serif", "DejaVu Sans Mono", "Myanmar 3"};
	private int currentFontIdx = 0;
	private TextView mTextView;
	private EditText mEntryEditText;
    private Button mOkButton;
    private Button mCancelButton;
    private Button mClearButton;
    private Typeface face;
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                        
        mTextView = (TextView)findViewById(R.id.textView);
        mEntryEditText = (EditText) findViewById(R.id.entry);
        mOkButton = (Button) findViewById(R.id.ok);
        mCancelButton = (Button) findViewById(R.id.cancel);
        mClearButton = (Button) findViewById(R.id.clear);
        
        currentFontIdx = 0;
        face=Typeface.createFromAsset(getAssets(), (String) fontPaths[currentFontIdx]); 
        mTextView.setTypeface(face);
        mEntryEditText.setTypeface(face);
        mEntryEditText.setInputType(0);
        
        mEntryEditText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	mEntryEditText.performLongClick();
           }
        });
        
        mOkButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String enteredText = mEntryEditText.getText().toString();
                mTextView.setText(enteredText);
                mEntryEditText.setText("");
           }
        });
        
        mCancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mEntryEditText.setText("");
           }
        });
        
        mClearButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	mTextView.setText("");
           }
        });
    }
    
    // Creates the menu items
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }
    
    // Handles item selections 
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.choose_font:
        	showDialog(0);
            return true;
        case R.id.quit:
            finish();
            return true;
        }
        return false;
    }

    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case 0:
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setTitle("Pick a font");
    		builder.setSingleChoiceItems(fontNames, currentFontIdx, new DialogInterface.OnClickListener() {
    		    public void onClick(DialogInterface dialog, int item) {
    		        currentFontIdx = item;
    		        face=Typeface.createFromAsset(getAssets(), (String) fontPaths[currentFontIdx]); 
		        	mTextView.setTypeface(face);
		        	mEntryEditText.setTypeface(face);
		        	dialog.cancel();
    		    }
    		});
    		AlertDialog alert = builder.create();
    		dialog = alert;
            break;
        default:
            dialog = null;
        }
        return dialog;
    }
}